import { _decorator, Component, Node } from 'cc';
import { ReactiveNumber } from '../../../scripts/libs/rxjs/cc3/ReactiveNumber';
import { BaseView } from '../../../scripts/libs/rxjs/cc3/BaseView';
import { find } from 'cc';
import { ParticleSystem2D } from 'cc';
import { Colors } from '../../scripts/game/basic';
import { Timer } from '../../../scripts/libs/rxjs/cc3/CreatorSchedule';
import { Subscription } from 'rxjs';
import { nullifySubscription } from '../../../scripts/libs/rxjs/utils';
import { ResourceHelper } from '../../scripts/ResourceHelper';
const { ccclass, property } = _decorator;

@ccclass('Burst0')
export class Burst0 extends BaseView {

    @property(ReactiveNumber)
    _colorId: ReactiveNumber = ReactiveNumber.DefaultValue(0);
    @property
    get colorId(): number { return this._colorId.value }
    set colorId(v: number) { this._colorId.Value = v; }

    useObserves() {
        return [
            this._colorId.subscribe(this.refreshColor.bind(this)),
        ];
    }

    onDestroyView(): void {
        nullifySubscription.call(this, '_sub');
    }

    refreshColor() {
        let ps2d = find('dot', this.node).getComponent(ParticleSystem2D);
        ps2d.startColor = Colors[this.colorId];
        ps2d.endColor = Colors[this.colorId];
    }

    _sub: Subscription = null;
    burst() {
        nullifySubscription.call(this, '_sub');
        let ps2d = find('dot', this.node).getComponent(ParticleSystem2D);
        ps2d.resetSystem();
        this._sub = Timer(.5, this.node).subscribe(_ => {
            ResourceHelper.Burst0Pool.putNode(this.node);
        });
    }
}

